package com.qf.quduba.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.quduba.VO.ResultVO;
import com.qf.quduba.entity.Consume;
import com.qf.quduba.entity.ModulVO;
import com.qf.quduba.entity.User;
import com.qf.quduba.service.BookService;
import com.qf.quduba.service.ConsumeService;
import com.qf.quduba.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者：程意
 * 时间：2020/10/14 15:10
 */
@RestController
@CrossOrigin
public class ConsumeController {
    @Resource
    private ConsumeService consumeService;
    @Resource
    private BookService bookService;
    @Resource
    private UserService userService;

    @GetMapping("consumeList")
    public Map<String, Object> findUserConsume(@RequestParam(value = "page", defaultValue = "1", required = false)Integer page,@RequestParam(value = "limit", defaultValue = "10", required = false)Integer limit,String userId){
        int userId1 = Integer.parseInt(userId);
        PageHelper.startPage(page, limit); // 使用PageHelper插件实现分页效果
        List<Consume> consumeList = consumeService.findUserConsume(userId1);
        PageInfo pageInfo = new PageInfo(consumeList); // 使用PageInfo对象封装查询结果
        Map<String, Object> map = new HashMap<>();
        map.put("data", pageInfo.getList()); // 得到当前页的数据
        map.put("code", 0);
        map.put("count", pageInfo.getTotal()); // 得到总条数
        return map;
    }

    @PostMapping("/deleteConsumes")
    public ResultVO deleteConsume(@RequestBody ModulVO modulVO){
        int i = consumeService.deleteConsumes(modulVO);
        if(i > 0){
            return new ResultVO(1000,"成功",null);
        }else{
            return new ResultVO(1001,"失败",null);
        }
    }

    @PostMapping("/deleteConsume")
    public ResultVO deleteConsume(Integer consumeId){
        int i = consumeService.deleteConsume(consumeId);
        if(i > 0){
            return new ResultVO(1000,"成功",null);
        }else{
            return new ResultVO(1001,"失败",null);
        }
    }

    //添加一条消费记录
    @PostMapping("addConsume")
    public ResultVO addConsume(@RequestBody Consume consume){
            User user = userService.selectUser(consume.getUserId());
            Double money = user.getMoney();
            if(money>=consume.getConsumeAmount()){
                Double newMoney=money-consume.getConsumeAmount();
                userService.updateUserMoney(consume.getUserId(),newMoney);
                if(consume.getConsumeType()==1) {
                    bookService.updateReward(Integer.toString(consume.getBookId()),consume.getConsumeAmount());
                }
                int i = consumeService.saveConsume(consume);
                if(i > 0){
                    return new ResultVO(1000,"成功",null);
                }else{
                    return new ResultVO(1001,"失败",null);
                }
            }
            return new ResultVO(1001,"您的余额不足，请充值！",null);
    }

    @GetMapping("consumeOne")
    public ResultVO findConsumeOne(Integer userId,Integer chapterId){
        Consume consume = consumeService.findConsumeByUserIdChapterId(userId, chapterId);
        if(consume != null){
            return new ResultVO(1000,"您已购买该章节",consume);
        }else{
            return new ResultVO(1001,"对不起，请购买该章节",null);
        }
    }

}
